{include 'header.tpl'}
<link href="/admin/css/goods.css" rel="stylesheet" >
<form method="get" action="/adminmall/spec/index" class="form-inline">
    <table>
        <tr>
            <td>
                <label for="form-w" class="control-label">规格名称：</label>
                <input class="form-control" name="name" value="{$smarty.get.name}" placeholder="" size="6"/>
            </td>
            <td>
                <button type="submit" class="btn btn-primary">查找</button>
                <button type="button" class="btn btn-success" id="btn-add">添加新规格</button>
            </td>
        </tr>
    </table>
</form>
<div class="clearfix" style="height:30px;"></div>
<table class="fTable" style="width: 100%;">
    <tr>
        <td>ID</td>
        <td>规格名称</td>
        <td>规格列表</td>
        <td>备注</td>
        <td>创建时间</td>
        <td>操作</td>
    </tr>
    {foreach from=$dataList item=item}
        <tr>
            <td>{$item.id}</td>
            <td>{$item.name}</td>
            <td>{$item.value}</td>
            <td>{$item.remark}</td>
            <td>{$item.create_time}</td>
            <td>
                <a class="edit" data-id="{$item.id}" href="javascript:;">修改</a>
                <a class="del" data-id="{$item.id}" href="javascript:;">删除</a>
            </td>
        </tr>
    {/foreach}
</table>
{include "pager.tpl"}
<script type="text/javascript">
    $(function () {
        create_date_picker();
        $('.del').click(function () {
            var id = $(this).data('id');
            if(confirm('确定要删除此域名？')){
                window.location.href = '/spec/del?id=' + id;
            }
        });
        $('#btn-add').click(function () {
            $.dialog({
                id : 'addSpec',
                title : '添加新规格',
                async : false,
                min_width: 600,
                min_height: 350,
                auto_close: false,
                content : function(){
                    var content;
                    $.post("/goods/specAdd",function(data){
                        content = data;
                    });
                    return content;
                },
                ok : function(target){
                    var status = true;
                    var params = { };
                    params.name = $.trim($('#spec_name').val());
                    if(params.name == ''){
                        return layer.msg('规格名称不能为空');
                    }
                    if($.isRepeat(params.value) === true){
                        return layer.msg('规格值不能重复');
                    }
                    params.remark = $.trim($('#spec_show').val());
                    params.value = [];
                    $(target).find('table .spec_value').each(function(index){
                        params.value[index] = $(this).val();
                    });
                    $.post("/spec/add",params,function(result){
                        if(result.code == 1){
                            target.close();//关闭弹窗
                            window.location.reload();
                        }else{
                            layer.msg(result.msg);
                            status = false;
                        }
                    });
                    return status;
                }
            });
        });
        $('.edit').click(function () {
            var id = $(this).data('id');
            $.dialog({
                id : 'editSpec',
                title : '添加新规格',
                async : false,
                min_width: 600,
                min_height: 350,
                auto_close: false,
                content : function(){
                    var content;
                    $.post("/goods/specEdit",{ id:id},function(data){
                        content = data;
                    });
                    return content;
                },
                ok : function(target){
                    var status = true;
                    var params = { };
                    params.id = id;
                    params.name = $.trim($('#spec_name').val());
                    if(params.name == ''){
                        return layer.msg('规格名称不能为空');
                    }
                    params.remark = $.trim($('#spec_show').val());
                    params.value = [];
                    $(target).find('table .spec_value').each(function(index){
                        params.value[index] = $(this).val();
                    });
                    if($.isRepeat(params.value) === true){
                        return layer.msg('规格值不能重复');
                    }
                    $.post("/spec/edit",params,function(result){
                        if(result.code == 1){
                            target.close();//关闭弹窗
                            window.location.reload();
                        }else{
                            layer.msg(result.msg);
                            status = false;
                        }
                    });
                    return status;
                }
            });
        });
    });
</script>
{include 'footer.tpl'}